package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.ReasonAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;

import java.util.ArrayList;
import java.util.List;



public class SubReasonActivity extends AppCompatActivity {

  //  @Bind(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
   // @Bind(R.id.toolbar)
    Toolbar mToolbar;
  //  @Bind(R.id.reasonListView)
    ListView mReasonListView;
    List mReasonList = null;
    ReasonBen mReasonBen=null;
    int reasonType=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_reason);
       // ButterKnife.bind(this);
        mTvToolbarTitle=(TextView)findViewById(R.id.tv_toolbar_title);
        mToolbar=(Toolbar)findViewById(R.id.toolbar);
        mReasonListView=(ListView)findViewById(R.id.reasonListView);

        mTvToolbarTitle.setText("具体原因");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mReasonBen = (ReasonBen) getIntent().getSerializableExtra("subReason");
        if(mReasonBen.getSub()!=null) {
            updateReasonList(mReasonBen.getSub());
        }
        reasonType = getIntent().getIntExtra("reasonType",-1);
    }

    private void updateReasonList(List dataSource) {
        mReasonList = new ArrayList<>();
        mReasonList.addAll(dataSource);
        mReasonListView.setAdapter(new ReasonAdapter(this,mReasonList));
        mReasonListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                ReasonBen item = (ReasonBen) mReasonList.get(position);
                if(reasonType==1) {
                    Intent intent = new Intent(SubReasonActivity.this, PutOutActivity.class);
                    intent.putExtra("reasonId", item.getId());
                    intent.putExtra("content", mReasonBen.getName() + item.getName());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                    finish();
                }else if(reasonType==0)
                {
                    Intent intent = new Intent(SubReasonActivity.this, repairActivity.class);
                    intent.putExtra("reasonId", item.getId());
                    intent.putExtra("content", mReasonBen.getName() + item.getName());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                    finish();
                }else
                {
                    Intent intent = new Intent(SubReasonActivity.this, KeepActivity.class);
                    intent.putExtra("reasonId", item.getId());
                    intent.putExtra("content", mReasonBen.getName() + item.getName());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
}
